<?php

    if(!defined('BASEPATH'))
        exit('No direct script access allowed');
		
	class Destinations extends CI_Controller
    {
		function __construct()
        {
            parent::__construct();
			
			$this->load->library ( 'rest', array (
                'server' => REST_URL_HOTELS,
                'http_user' => 'cipa',
                'http_pass' => 'booking',
                'http_auth' => 'basic' // or 'digest' 
            ) );			
			
            $this->load->library ( 'session' );
            $this->load->library ( 'template' );
            $this->load->helper ( 'clean_html' );
            $this->load->helper('urlify');
            $this->lang->load ( 'common' );
            $this->lang->load ( 'destinations' );
            $this->template->set_view ( "destinations" );
			
			require_once("urlify.php");
			
			$this->data['host_url'] = HOST_URL_HOTELS;
			
			$allowedDestinations = array(
				"2" => "Budva",
				"3" => "Herceg Novi",
				"4" => "Kotor",
				"5" => "Tivat",
				"6" => "Ulcinj",
				"14" => "Sv. Stefan",
				"15" => "Petrovac",
				"45" => "Ada Bojana",
				"53" => "Dubrovnik",
				"1" => "Bar",
				"54" => "Ostrog Monastery",
				"55" => "Budva Old Town",
				"48" => "Albania",
			);
			$destinations = $this->rest->post ( 'destinationList', array ('lang' => $this->lang->lang ()) );
			$filtered = array();
			foreach($destinations as $destination) {
				if(isset($allowedDestinations[(string)$destination->ID])) {
					$filtered[] = $destination;
				}
			}			

			$this->data['destinations'] = $filtered;
		}

		public function index()
        {		
			$this->data['title'] = lang ( 'title-index' );
            $this->data['description'] = lang ( 'description-index' );
            $this->data['canonical'] = 'http://globtour.me/' . $this->lang->lang () . '/destinations';
			
			$this->setFooterDestinations();
			
			$this->template->load ( 'destinations/index', $this->data );
		}	

		public function details($href) 
		{
			$destination_ID=idFromUrl($href, '-');
			$this->data['destination'] = $this->rest->post ( 
				'destinationDetails', array (
					'lang' => $this->lang->lang (),
					'destination_id' => $destination_ID,
				) );
			$this->data['title']= str_replace('{|0|}', $this->data['destination']->name, lang('title-details'));			
			$this->data['description']= clean_html($this->data['destination']->information);		
			
			$this->setFooterDestinations($this->data['destination']->ID);
			
			$this->template->load ( 'destinations/details', $this->data );
		}
		
		function setFooterDestinations($current = -1) {
			$this->data['action_1'] = $this->data['destinations'][array_rand($this->data['destinations'])];
			$this->data['action_2'] = $this->data['destinations'][array_rand($this->data['destinations'])];
		}
	}
?>